﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SysCommon.WinServices
{
    /// <summary>
    /// 同步配置信息获取
    /// </summary>
    public class SyncFrequencyConfig
    { 
        /// <summary>
        /// 计算执行的间隔时间
        /// </summary>
        /// <param name="nowTime">当前时间</param>
        /// <param name="runs">上面程序运行了多少时间</param>
        /// <param name="sm">间隔时间的设置</param>
        /// <returns>返回间隔时间毫秒</returns>
        public static int GetConfIntervalSeconds(DateTime begin, SyncFrequencyModel sm)
        {
            DateTime nowTime = DateTime.Now;
            int runs = (int)((nowTime - begin).TotalSeconds);
            if (sm.IntervalSeconds > 0 && sm.PMIntervalSeconds == 0)
            {
                sm.PMIntervalSeconds = sm.IntervalSeconds;
            }
            if (sm.IntervalSeconds == 0 && sm.PMIntervalSeconds == 0)
            {
                return 864000 * 1000; //如果间隔时间为0则默认为间隔一天执行一次
            }
            else if (nowTime.Hour < 13)
            {
                if (sm.IntervalSeconds == 0)
                {
                    DateTime dt = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 13, 0, 0);
                    int p = (int)((dt - nowTime).TotalSeconds);
                    return p * 1000; //如果间隔时间为0则默认为当天下午的13点执行一次
                }
                else
                    if (runs > sm.IntervalSeconds)
                    {
                        return 1000;
                    } //如果同步数据的时间超过间隔时间则休息1秒钟
                    else
                    {
                        int c = 1000 + (sm.IntervalSeconds - runs) * 1000;
                        return c;//间隔时间减去运行用的时间再加1秒钟为系统间隔时间
                    }
            }//上午数据同步频率
            else
            {
                if (sm.PMIntervalSeconds == 0)
                {
                    DateTime dt = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 23, 59, 59);
                    int p = (int)((dt - nowTime).TotalSeconds);
                    return p * 1000; //如果间隔时间为0则默认为当天晚上的12点执行一次
                }
                else if (runs > sm.PMIntervalSeconds)
                {
                    return 1000;
                } //如果同步数据的时间超过间隔时间则休息1秒钟
                else
                {
                    int c = 1000 + (sm.IntervalSeconds - runs) * 1000;
                    return c;//间隔时间减去运行用的时间再加1秒钟为系统间隔时间
                }
            }

        }
        /// <summary>
        /// 从配置文件中获取配置信息
        /// </summary>
        /// <param name="apiName">API函数名称</param>
        /// <returns>返回一个实体</returns>
        public static SyncFrequencyModel GetModelConfig(string apiName)
        {
            SyncFrequencyModel sm = new SyncFrequencyModel();
            sm.APIName = apiName;
            string str = ConfigurationManager.AppSettings[apiName];
            if (!string.IsNullOrEmpty(str))
            {
                string[] ins = str.Split('|');
                if (ins.Length > 0)
                {
                    int i = 0;
                    int.TryParse(ins[0], out i);
                    sm.IntervalSeconds = i;
                    if (ins.Length > 1)
                    {
                        int j = 0;
                        int.TryParse(ins[0], out j);
                        sm.PMIntervalSeconds = j;
                    }
                }
            }
            return sm;
        }

    }
}
